using Senparc.Weixin.Entities;
using Senparc.Weixin.Helpers;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.MP.AdvancedAPIs.GroupMessage;
using Senparc.Weixin.MP.AdvancedAPIs.Media;
using Senparc.Weixin.MP.CommonAPIs;
using System;
using System.Collections.Generic;
using System.IO;
namespace Senparc.Weixin.MP.AdvancedAPIs
{
	public static class MediaApi
	{
		public static UploadTemporaryMediaResult UploadTemporaryMedia(string accessTokenOrAppId, UploadMediaFileType type, string file, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<UploadTemporaryMediaResult>(delegate(string accessToken)
			{
				string url = string.Format("http://api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", accessToken, type.ToString());
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary["media"] = file;
				return Post.PostFileGetJson<UploadTemporaryMediaResult>(url, null, dictionary, null, null, timeOut);
			}, accessTokenOrAppId, true);
		}
		public static UploadTemporaryMediaResult UploadTemporaryNews(string accessTokenOrAppId, int timeOut = 10000, params NewsModel[] news)
		{
			return ApiHandlerWapper.TryCommonApi<UploadTemporaryMediaResult>(delegate(string accessToken)
			{
				var data = new
				{
					articles = news
				};
				return CommonJsonSend.Send<UploadTemporaryMediaResult>(accessToken, "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token={0}", data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static void Get(string accessToken, string mediaId, Stream stream)
		{
			string url = string.Format("http://api.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}", accessToken, mediaId);
			Senparc.Weixin.HttpUtility.Get.Download(url, stream);
		}
		public static UploadForeverMediaResult UploadNews(string accessTokenOrAppId, int timeOut = 10000, params NewsModel[] news)
		{
			return ApiHandlerWapper.TryCommonApi<UploadForeverMediaResult>(delegate(string accessToken)
			{
				var data = new
				{
					articles = news
				};
				return CommonJsonSend.Send<UploadForeverMediaResult>(accessToken, "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token={0}", data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static UploadForeverMediaResult UploadForeverMedia(string accessTokenOrAppId, string file, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<UploadForeverMediaResult>(delegate(string accessToken)
			{
				string url = string.Format("https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}", accessToken);
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary["media"] = file;
				return Post.PostFileGetJson<UploadForeverMediaResult>(url, null, dictionary, null, null, timeOut);
			}, accessTokenOrAppId, true);
		}
		public static UploadForeverMediaResult UploadForeverVideo(string accessTokenOrAppId, string file, string title, string introduction, int timeOut = 40000)
		{
			return ApiHandlerWapper.TryCommonApi<UploadForeverMediaResult>(delegate(string accessToken)
			{
				string url = string.Format("https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}", accessToken);
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary["media"] = file;
				dictionary["description"] = string.Format("{{\"title\":\"{0}\", \"introduction\":\"{1}\"}}", title, introduction);
				return Post.PostFileGetJson<UploadForeverMediaResult>(url, null, dictionary, null, null, timeOut);
			}, accessTokenOrAppId, true);
		}
		public static GetNewsResultJson GetForeverNews(string accessTokenOrAppId, string mediaId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<GetNewsResultJson>(delegate(string accessToken)
			{
				string urlFormat = "https://api.weixin.qq.com/cgi-bin/material/material?access_token={0}";
				var data = new
				{
					media_id = mediaId
				};
				return CommonJsonSend.Send<GetNewsResultJson>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static void GetForeverMedia(string accessToken, string mediaId, Stream stream)
		{
			string url = string.Format("https://api.weixin.qq.com/cgi-bin/material/material?access_token={0}", accessToken);
			var data = new
			{
				media_id = mediaId
			};
			SerializerHelper serializerHelper = new SerializerHelper();
			string jsonString = serializerHelper.GetJsonString(data);
			Post.Download(url, jsonString, stream);
		}
		public static WxJsonResult DeleteForeverMedia(string accessTokenOrAppId, string mediaId, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				string urlFormat = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token={0}";
				var data = new
				{
					media_id = mediaId
				};
				return CommonJsonSend.Send<WxJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static WxJsonResult UpdateForeverNews(string accessTokenOrAppId, string mediaId, int? index, NewsModel news, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<WxJsonResult>(delegate(string accessToken)
			{
				string urlFormat = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token={0}";
				var data = new
				{
					media_id = mediaId,
					index = index,
					articles = news
				};
				return CommonJsonSend.Send<WxJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static GetMediaCountResultJson GetMediaCount(string accessTokenOrAppId)
		{
			return ApiHandlerWapper.TryCommonApi<GetMediaCountResultJson>(delegate(string accessToken)
			{
				string url = string.Format("https://api.weixin.qq.com/cgi-bin/material/materialcount?access_token={0}", accessToken);
				return Senparc.Weixin.HttpUtility.Get.GetJson<GetMediaCountResultJson>(url, null);
			}, accessTokenOrAppId, true);
		}
		public static MediaList_NewsResult GetNewsMediaList(string accessTokenOrAppId, int offset, int count, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<MediaList_NewsResult>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://api.weixin.qq.com/cgi-bin/material/batchmaterial?access_token={0}", accessToken);
				var data = new
				{
					type = "news",
					offset = offset,
					count = count
				};
				return CommonJsonSend.Send<MediaList_NewsResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static MediaList_OthersResult GetOthersMediaList(string accessTokenOrAppId, UploadMediaFileType type, int offset, int count, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<MediaList_OthersResult>(delegate(string accessToken)
			{
				string urlFormat = string.Format("https://api.weixin.qq.com/cgi-bin/material/batchmaterial?access_token={0}", accessToken);
				var data = new
				{
					type = type.ToString(),
					offset = offset,
					count = count
				};
				return CommonJsonSend.Send<MediaList_OthersResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut, false);
			}, accessTokenOrAppId, true);
		}
		public static UploadImgResult UploadImg(string accessTokenOrAppId, string file, int timeOut = 10000)
		{
			return ApiHandlerWapper.TryCommonApi<UploadImgResult>(delegate(string accessToken)
			{
				string url = string.Format("https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={0}", accessToken);
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary["media"] = file;
				return Post.PostFileGetJson<UploadImgResult>(url, null, dictionary, null, null, timeOut);
			}, accessTokenOrAppId, true);
		}
	}
}
